<template>
  <div class="xtx-goods-page" v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem>
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="/">
          {{ goods.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 新增结构 -->
        <!-- 图片预览区 -->
        <div class="media">
          <XtxGoodViewer :imageList="goods.mainPictures" />
          <!-- 销量组件 -->
          <GoodsSales :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName :goods="goods" />
          <!-- 新增 sku -->
          <XtxSku :goods="goods" @change="getSku" />
          <!-- 数量加减组件 -->
          <!-- 1. 子传父把count变量同步给buyCount -->
          <!-- <XtxNumbox :max="goods.inventory" @getCount="getCount"/> -->
          <!-- 2. v-model把count变量同步给buyCount
          model语法糖:
          vue2 => :value + @input
          vue3 => :modelValue(父串子) + @update:modelValue(子传父)
           -->
          <XtxNumbox :max="goods.inventory" v-model="buyCount" @update:modelValue="getCount"/>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoodsApi } from '@/api/goods'
import { ref, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsSales,
    GoodsName
  },
  setup () {
    // 商品详情数据
    const goods = ref({})
    provide('goods', goods)
    // 获取详情数据方法
    const route = useRoute()
    const getGoods = async () => {
      const { result } = await findGoodsApi(route.params.id)
      goods.value = result
    }
    getGoods()
    // 获取sku数据
    const getSku = (sku) => {
      console.log('sku:', sku)
      // 修改商品的现价原价库存信息
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
    }
    const buyCount = ref(10)
    const getCount = (num) => {
      buyCount.value = num
    }
    return { goods, getSku, getCount, buyCount }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
